﻿// E-Book Copier
// Copyright (C) 2009 Chuan-Gen Fan
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
// 
// Project: http://code.google.com/p/ebookcopier/
// Contact Author:
//    website: http://chuangen.name/
// e-mail/msn: chuangen@live.cn

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace EbookCopier
{
    /// <summary>
    /// PDF 文档生成进度对话框。
    /// </summary>
    public partial class PdfProcessForm : Form
    {
        BackgroundWorker bw = null;
        /// <summary>
        /// 初始化类 <see cref="PdfProcessForm"/> 的新实例。
        /// </summary>
        /// <param name="bw">BackgroundWorker 实例。</param>
        public PdfProcessForm(BackgroundWorker bw)
        {
            InitializeComponent();

            this.bw = bw;
            bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
            bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);

            btnCancel.Click += new EventHandler(btnCancel_Click);
        }

        void btnCancel_Click(object sender, EventArgs e)
        {
            bw.CancelAsync();
        }
        /// <summary>
        /// 获取或设置处理进度（百分数）。
        /// </summary>
        /// <value>进度（百分数）</value>
        public int ProgressPercentage
        {
            get { return progressBar.Value; }
            set
            {
                if(progressBar.Value == value)
                    return;

                if(value < 0)
                    progressBar.Value = 0;
                else if(value > 100)
                    progressBar.Value = 100;
                else
                    progressBar.Value = value;

                labelTitle.Text = string.Format("正在生成 PDF 文档，请稍等 ...({0} %)", progressBar.Value);
            }
        }

        void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.ProgressPercentage = 100;
            if (e.Cancelled)
                labelTitle.Text = "操作已经取消。";
            else if (e.Error != null)
                labelTitle.Text = "生成过程中发生错误。";
            else
                labelTitle.Text = "成功创建 PDF 文档。";
        }

        void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            this.ProgressPercentage = e.ProgressPercentage;
        }
    }
}